﻿using Microsoft.Practices.Unity;
using UnityDemo.LoggingUtility;
using UnityDemo.Models;

namespace UnityDemo.RegAndResCode
{
    /// <summary>
    /// This sample shows how to Register an interface or class resolving it
    /// </summary>
    internal class RegisterAndResolveInterfacesAndClass : ISample
    {
        public RegisterAndResolveInterfacesAndClass()
        {
            InterfacesAndClassDemo();
            ClassRegistrationDemo();
            RegisterClassInstance();
            RegisterInterfaceInstance();
        }

        /// <summary>
        /// This sample shows how to Register an interface and its implementing class and resolving it
        /// </summary>
        private void InterfacesAndClassDemo()
        {
            var container = new UnityContainer();

            //Registering IDuck and its implementing class. This registration is a Default registration
            container.RegisterType<IDuck, KingDuck>();

            //Registering IDuck and its implementing class. This registration is a Default registration
            container.RegisterType<IQuackStrategy, LoudQuack>();

            //Resolving instance of IDuck
            var duck = container.Resolve<IDuck>();

            LogerInstance.Logger.Write(duck);

            //Resolving instance of IQuackStrategy
            var quack = container.Resolve<IQuackStrategy>();

            LogerInstance.Logger.Write(quack);
        }

        /// <summary>
        /// This sample shows how to an unregistered class is still resolved
        /// </summary>
        private void ClassRegistrationDemo()
        {
            var container = new UnityContainer();

            //Resolving instance of KingDuck without registering it
            var duck = container.Resolve<KingDuck>();

            LogerInstance.Logger.Write(duck);

            //Resolving instance of without registering it
            var quack = container.Resolve<LoudQuack>();

            LogerInstance.Logger.Write(quack);
        }

        /// <summary>
        /// This sample show's how to register an instance of a class with the container and see it being used
        /// </summary>
        private void RegisterClassInstance()
        {
            var container = new UnityContainer();

            //Resolving instance of KingDuck without registering it
            var duck = new KingDuck();
            duck.Name = "Supreme King Duck";
            LogerInstance.Logger.Write(duck.Name);

            //Resolving instance of without registering it
            container.RegisterInstance(duck);

            //Resolving instance of without registering it
            var duck2 = container.Resolve<KingDuck>();

            LogerInstance.Logger.Write(duck2.Name);
        }

        /// <summary>
        /// This sample show's how to register an instance of an Interface and its class with the container and see it being used
        /// </summary>
        private void RegisterInterfaceInstance()
        {
            var container = new UnityContainer();

            //Resolving instance of KingDuck without registering it
            IDuck duck = new KingDuck();
            duck.Name = "Supreme King Duck";

            LogerInstance.Logger.Write(duck.Name);

            container.RegisterInstance(duck);

            //Resolving instance of without registering it
            var duck2 = container.Resolve<IDuck>();

            LogerInstance.Logger.Write(duck2.Name);
            duck2.Name = "Change in King Duck";
            container.RegisterInstance(duck2);

            //Resolving instance of without registering it
            var duck3 = container.Resolve<IDuck>();

            LogerInstance.Logger.Write(duck3.Name);
        }
    }
}